const USER_INFO_KEY = 'user-info';
const TOKEN_KEY = 'token';

export default {
  namespaced: true,
  state: () => {
    return {
      // token
      token: uni.getStorageSync(TOKEN_KEY) || null,
      // 用户信息
      userInfo: uni.getStorageSync(USER_INFO_KEY) || {}
    };
  },
  mutations: {
    setToken(state, token) {
      state.token = token;
      uni.setStorage({
        key: TOKEN_KEY,
        data: state.token
      });
    },
    removeToken(state) {
      state.token = null;
      uni.setStorage({
        key: TOKEN_KEY,
        data: state.token
      });
    },
    setUserInfo(state, userInfo) {
      state.userInfo = userInfo;
      uni.setStorage({
        key: USER_INFO_KEY,
        data: state.userInfo
      });
    },
    removeUserInfo(state) {
      state.userInfo = {};
      uni.setStorage({
        key: USER_INFO_KEY,
        data: state.userInfo
      });
    }
  }
};
